let Cesium:any=undefined;

//城市警示墙
export class WarnLinkMaterialProperty extends Cesium.Property {
    private _definitionChanged: any;
    private _color: any;
    private _colorSubscription: any;
    private _time: any;
    private _duration: any;

    constructor(options: any) {
        super()
        options = Cesium.defaultValue(options, Cesium.defaultValue.EMPTY_OBJECT);
        this._definitionChanged = new Cesium.Event();
        this._color = options.color || Cesium.Color.BLUE;
        this._duration = options.duration || 3000;
        this._time = new Date().getTime();
    }

    get isvarant() {
        return false;
    }

    get definitionChanged() {
        return this._definitionChanged;
    }

    set color(value: any) {
        this._color = value;
    }

    get color() {
        return this._color;
    }

    getType() { //'WarnWallLinkType'
        // @ts-ignore
        return Cesium.Material.WarnLinkType;
    }

    getValue(time: any, result: any) {
        if (!Cesium.defined(result)) {
            result = {};
        }
        result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
        // @ts-ignore
        result.image = Cesium.Material.WarnLinkImage;
        result.time = ((new Date().getTime() - this._time) % this._duration) / this._duration;
        return result;
    }

    equals(other: any) {
        return this === other || (other instanceof WarnLinkMaterialProperty && Cesium.Property.equals(this._color, other._color));
    }

}

function getDfSt(dfSt: any, attrArr: any) {
    if (dfSt) {
        const res = attrArr.reduce((pre: any, curr: any) => {
            if (pre) {
                return pre[curr];
            } else {
                return dfSt[curr];
            }
        }, undefined);
        return dfSt.baseService + res;
    }
    return undefined;
}

//带方向的墙体
function _getDirectionWallShader(options: any) {
    if (options && options.get) {
        var materail =
            "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
                      {\n\
                      czm_material material = czm_getDefaultMaterial(materialInput);\n\
                      vec2 st = materialInput.st;\n\
                      \n ";
        if (options.freely == "vertical") {
            //（由下到上）
            materail +=
                "vec4 colorImage = texture(image, vec2(fract(float(" +
                options.count +
                ")*st.t " +
                options.direction +
                " time), fract(st.s)));\n ";
        } else {
            //（direction顺[-]/逆[+]时针）
            materail +=
                "vec4 colorImage = texture(image, vec2(fract(float(" +
                options.count +
                ")*st.s " +
                options.direction +
                " time), fract(st.t)));\n ";
        }
        //泛光
        materail +=
            "vec4 fragColor;\n\
                      fragColor.rgb = (colorImage.rgb+color.rgb) / 1.0;\n\
                      fragColor = czm_gammaCorrect(fragColor);\n ";

        materail +=
            " material.diffuse = colorImage.rgb;\n\
                      material.alpha = colorImage.a;\n\
                      material.emission = fragColor.rgb;\n\
                      \n\
                      return material;\n\
                      }\n\
                      ";

        return materail;
    }
}

const _installWarnMaterial = (dfSt: any,CesiumSources:any) => {
    Cesium=CesiumSources;
    // @ts-ignore
    Cesium.Material.WarnLinkType = "WarnWallLinkType";
    // @ts-ignore
    Cesium.Scene.WarnLinkMaterialProperty = WarnLinkMaterialProperty;
    // @ts-ignore
    Cesium.Material.WarnLinkImage =
        getDfSt(dfSt, ["material", "WarnLinkMaterialProperty"]) || "static/data/images/Textures/jsx2.png";
    // @ts-ignore
    Cesium.Material._materialCache.addMaterial(Cesium.Material.WarnLinkType, {
        fabric: {
            // @ts-ignore
            type: Cesium.Material.WarnLinkType,
            uniforms: {
                color: new Cesium.Color(1.0, 0.0, 0.0, 0.5),
                // @ts-ignore
                image: Cesium.Material.WarnLinkImage,
                time: 0,
            },
            source: _getDirectionWallShader({
                get: true,
                count: 10.0,
                freely: "cross",
                direction: "-",
            }),
        },
        translucent: function () {
            return true;
        },
    });
}
export {
    _installWarnMaterial
};

